package com.party.service.impl;

import java.util.Date;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.party.domain.Signin;
import com.party.domain.User;
import com.party.domain.UserRole;
import com.party.mapper.UserMapper;
import com.party.service.UserService;
import com.party.tool.MD5;
import com.party.tool.javaMail;



@Service
public class UserServiceImpl implements UserService
{
	@Autowired
	private UserMapper partymapper;
	
	private MD5 md5=new MD5();
	
	@Override
	public int userRegister(User user) 
	{

		partymapper.inster(user);
		return user.getId();
	}
	
	public void addUserRole(int uid)
	{
		UserRole urs=new UserRole();
		urs.setUser_id(uid);
		urs.setRole_id(2);
		partymapper.insterUR(urs);
	}
	
	public void changeUserIcon(int id,String icon)
	{
		User user=partymapper.selectIdUserMessage(id);
		user.setIcon(icon);
		partymapper.updateIcon(user);
	}
	
	
	
	//增加管理员帐号接口
	public int addAdminUser(User user)
	{
		user.setPassword(md5.toMD5(user.getPassword()));
		user.setState("success");
		user.setCode(1);
		user.setIntegral(0);
		user.setIcon("d:\\");
		user.setTime(new java.sql.Date(new java.util.Date().getTime()));
		partymapper.insterAdmin(user);
		return user.getId();
	}
	
	//增加管理员帐号权限
	public void addRoleAdmin(int uid)
	{
		UserRole urs=new UserRole();
		urs.setUser_id(uid);
		urs.setRole_id(1);
		partymapper.insterUR(urs);
	}

	
	//分页查询所有用户
	public List<User> findAllUser(int page,int size) throws Exception
	{
		int start=size*(page-1);
		List<User> user=partymapper.selectAllUser(start, size);
		if(user==null){
			try {
				throw new Exception("查询所有用户为空");
			} catch (Exception e) {
				
				e.printStackTrace();
			}
		}
		return user;
	}
	
	
	//管理员更新用户所有数据
	public void updateAllDate(User users,int role_id) throws Exception
	{
		User user=partymapper.selectIdUserMessage(users.getId());
		if (user == null) {
            throw new Exception("无id为 " + users.getId() + " 的用户");
        }	
		users.setPassword(MD5.toMD5(users.getPassword()));
		users.setTime(user.getTime());
		users.setIcon(user.getIcon());
		partymapper.updateUserAllDate(users);
		partymapper.updateUserRole(role_id, users.getId());
	}
	
	
	public User userREgisterInit(String Email,String Password,String Name,String Phone,String state)
	{
		User user=new User();
		//Phone = Phone.replaceAll("\"","");
		user.setEmail(Email);
		user.setPassword(MD5.toMD5(Password));
		user.setName(Name);
		user.setPhone(Phone);
		user.setState(state);
		user.setCode(0);
		user.setIntegral(0);
		user.setIcon("d:\\");
		user.setTime(new java.sql.Date(new java.util.Date().getTime()));
		//user.setRole("common");
		return user;
	}

	public String sendRegisterEmail(String Email)
	{
		javaMail jMail=new javaMail();		
		String state=jMail.activation(Email);
		return state;
	}
	
	public String sendPasswordEmail(String Email,String Password)
	{
		javaMail jMail=new javaMail();
		String state=jMail.changePWD(Email, Password);
		return state;
	}
	
	public String activateUser(String email)
	{
		partymapper.update(email);
		return "success";
	}
	
	//登录验证
	public User loginCheck(String Email,String Password)
	{
		
		Password=md5.toMD5(Password);
		List<User> user=partymapper.selectUser(Email);
		for(int i=0;i<user.size();i++)
		{
			if(user.get(i).getEmail().equals(Email)&&user.get(i).getPassword().equals(Password))
			{
				if(user.get(i).getCode()==0)
				{
					return null;
				}
				return user.get(i);
			}
		}
		
		return null;
	}
	
	//邮箱是否注册
	public User whetherRegister(String Email)
	{
		List<User> user=partymapper.selectUser(Email);
		for(int i=0;i<user.size();i++)
		{
			if(user.get(i).getEmail().equals(Email))
			{
				if(user.get(i).getCode()==0)
				{
					return null;
				}
				return user.get(i);
			}
		}
		
		return null;
	}

	public String ChangeUserPassword(String Email,String Password)
	{
		Password=md5.toMD5(Password);
		partymapper.updateIdUser(Email, Password);
		return "success";
	}
	
	
	public void changeUserDate(String phone,String name,int id)
	{
		User user=partymapper.selectIdUserMessage(id);
		user.setPhone(phone);
		user.setName(name);
		partymapper.updateUserDate(user);
	}
	
	
	public Object getUserMessage(int uid)
	{
		Object user=partymapper.selectUserMessage(uid);
		return user;
	}
	
	
	 //String转int
	 public int str_int(String str)
	 {
		 int i=Integer.parseInt(str);
		return i;
		 
	 }
	 
	 
	 
	 public Signin findSignin(int id)
	 {
		 Signin signin=partymapper.selectSignin(id);
		 return signin;
	 }
	 
	 public void insterSigninUser(int id,Date time)
	 {
		 Signin signin=new Signin();
		 signin.setUser_id(id);
		 signin.setTime(time);
		 partymapper.insterSignin(signin);
		 
		 User user=partymapper.selectIdUserMessage(id);
		 user.setIntegral(user.getIntegral()+1);
		 partymapper.updateIntegral(user);
		 
	 }
	 
	 
	 public boolean judgeTime(Date date1,Date date2)
	 {
		 long  between = date2.getTime() - date1.getTime();
		 if(between > (24* 3600000))
		 {
		     return true;
		 }
		 return false;
		 
	 }
	 public boolean  UpdateUserIntegral(int id)
	 {
		 User user=partymapper.selectIdUserMessage(id);
		
		 
		
		 Signin signin=partymapper.selectSignin(id);
		 Date date=new java.sql.Date(new java.util.Date().getTime());
		 if(this.judgeTime(signin.getTime(), date))
		 {
			 signin.setTime(date);
			 partymapper.updateTime(signin);
			 
			 user.setIntegral(user.getIntegral()+1);
			 partymapper.updateIntegral(user);
			 return true;
		 }
		 return false;
		 

	 }


	
}
